package com.zj.controller;

import com.zj.utils.DBUtils;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;

public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @SneakyThrows
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        Connection conn = DBUtils.getConnection();
        String sql = "insert into user(email,gender,password,username,telephone,introduce) values (?,?,?,?,?,?)";
        PreparedStatement statement = conn.prepareStatement(sql);
        statement.setString(1, req.getParameter("email"));
        statement.setString(2, req.getParameter("gender"));
        statement.setString(3, req.getParameter("password"));
        statement.setString(4, req.getParameter("username"));
        statement.setString(5, req.getParameter("telephone"));
        statement.setString(6, req.getParameter("introduce"));
        int i = statement.executeUpdate();
        if (i!=0){
            resp.getWriter().println("注册成功");
        }else
        {
            resp.getWriter().println("注册失败");
        }
        statement.close();
        conn.close();
    }
}
